package com.keensense.vcm.internal;

import static com.keensense.vcm.common.parser.ResponseParsers.vehicleSearchResponseParser;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.model.VehicleSearchByPhotoRequest;
import com.keensense.vcm.model.VehicleSearchRequest;
import com.keensense.vcm.model.VehicleSearchResponse;

/**
 * 车辆特征分析管理
 *
 * @author lanjian
 * @date 2020/5/6 4:03 下午
 */
public class VehicleAnalysisOperation extends VCMOperation {

    public VehicleAnalysisOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }

    /**
     * 根据特征搜索车辆
     *
     * @param vehicleSearchRequest
     * @return
     */
    public VehicleSearchResponse searchVehicle(VehicleSearchRequest vehicleSearchRequest) {
        String xml = getXmlFromObject(vehicleSearchRequest, VehicleSearchRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
            .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.VEHICLE_SEARCH);
        return doOperation(request, vehicleSearchResponseParser, true);
    }

    /**
     * 根据图片搜索车辆
     *
     * @param request
     * @return
     */
    public VehicleSearchResponse searchVehicleByPhoto(VehicleSearchByPhotoRequest request) {
        String xml = getXmlFromObject(request, VehicleSearchByPhotoRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage requestMessage = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
            .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        requestMessage.setResourcePath(VCMResourcePathConstants.VEHICLE_SEARCH_BY_PHOTO);
        return doOperation(requestMessage, vehicleSearchResponseParser, true);
    }

}
